package cn.daniellee.plugin.ice.command;

import cn.daniellee.plugin.ice.IceBaoxue;
import cn.daniellee.plugin.ice.core.BaoxueCore;
import cn.daniellee.plugin.ice.model.PlayerData;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class BaoxueCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (strings.length > 0) {
            if (commandSender.isOp() && "reload".equalsIgnoreCase(strings[0])) {
                IceBaoxue.getInstance().reloadConfig();
                IceBaoxue.getInstance().loadConfig();
                commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.reload-success", "&eConfiguration reload completed.")).replace("&", "§"));
                return true;
            }
            if (commandSender.hasPermission("ice.use") || commandSender.isOp()) {
                if ("toggle".equals(strings[0])) {
                    PlayerData playerData = IceBaoxue.getInstance().playerData.get(commandSender.getName());
                    if (playerData == null) {
                        int defaultPeriod = IceBaoxue.getInstance().getConfig().getInt("setting.default-period", 60);
                        String defaultSuction = IceBaoxue.getInstance().getConfig().getString("setting.default-suction", "m");
                        playerData = new PlayerData(commandSender.getName(), false, defaultPeriod, defaultSuction);
                        IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".period", defaultPeriod);
                        IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".suction", defaultSuction);
                    }
                    playerData.setEnable(!playerData.isEnable());
                    IceBaoxue.getInstance().playerData.put(commandSender.getName(), playerData);
                    IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".enable", playerData.isEnable());
                    commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.toggle-set", "&eSet up successfully, you {status} &euse of &bICE Vape &e.").replace("{status}", playerData.isEnable() ? IceBaoxue.getInstance().getConfig().getString("message.toggle-status.start", "&awill start") : IceBaoxue.getInstance().getConfig().getString("message.toggle-status.stop", "&cwill stop"))).replace("&", "§"));
                } else if ("period".equals(strings[0]) && strings.length > 1) {
                    int period = Integer.valueOf(strings[1]);
                    if (period > 0) {
                        PlayerData playerData = IceBaoxue.getInstance().playerData.get(commandSender.getName());
                        if (playerData == null) {
                            String defaultSuction = IceBaoxue.getInstance().getConfig().getString("setting.default-suction", "m");
                            playerData = new PlayerData(commandSender.getName(), true, period, defaultSuction);
                            IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".enable", true);
                            IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".suction", defaultSuction);
                        }
                        playerData.setPeriod(period);
                        playerData.setNextSpit(0); // 马上抽下一口
                        IceBaoxue.getInstance().playerData.put(commandSender.getName(), playerData);
                        IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".period", period);
                        commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.period-set", "&eSet up successfully, take a bite of &bICE Vape &eevery &b{second} &eseconds.").replace("{second}", String.valueOf(period))).replace("&", "§"));
                    } else commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.invalid-period", "&eInvalid use interval, please enter the correct number of seconds.")).replace("&", "§"));
                } else if ("suction".equals(strings[0]) && strings.length > 1) {
                    if (strings[1] != null && strings[1].length() == 1 && "smlv".contains(strings[1].toLowerCase())) {
                        String suction = strings[1].toLowerCase();
                        PlayerData playerData = IceBaoxue.getInstance().playerData.get(commandSender.getName());
                        if (playerData == null) {
                            int defaultPeriod = IceBaoxue.getInstance().getConfig().getInt("setting.default-period", 60);
                            playerData = new PlayerData(commandSender.getName(), true, defaultPeriod, suction);
                            IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".enable", true);
                            IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".period", defaultPeriod);
                        }
                        playerData.setSuction(suction);
                        playerData.setNextSpit(0); // 马上抽下一口
                        IceBaoxue.getInstance().playerData.put(commandSender.getName(), playerData);
                        IceBaoxue.getInstance().updatePlayerDataYaml(commandSender.getName() + ".suction", suction);
                        commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.suction-set", "&eSet up successfully, you will use &eICE Vape &ewith {suction} &esuction.").replace("{suction}", IceBaoxue.getInstance().getConfig().getString("message.suction-status." + suction, "m"))).replace("&", "§"));
                    } else commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.invalid-suction", "&eInvalid suction value, please enter one of s/m/l/v.")).replace("&", "§"));
                } else if ("violent".equals(strings[0]) && commandSender instanceof Player) {
                    BaoxueCore.useViolent((Player) commandSender);
                } else sendHelp(commandSender);
            } else commandSender.sendMessage((IceBaoxue.getInstance().getPrefix() + IceBaoxue.getInstance().getConfig().getString("message.no-permission", "&eYou don't have permission to do this.")).replace("&", "§"));
        } else sendHelp(commandSender);
        return true;
    }


    private void sendHelp(CommandSender commandSender) {
        commandSender.sendMessage(("&m&a----&m&6----------&b " + IceBaoxue.getInstance().getConfig().getString("prompt-prefix", "IceBaoxue") + " &m&6----------&m&a----").replace("&", "§"));

        String targetText = IceBaoxue.getInstance().getConfig().getString("help.toggle", "Start or stop use of ICE Vape!").replace("&", "§");
        TextComponent targetHelp = new TextComponent("/ice toggle  " + targetText);
        targetHelp.setColor(ChatColor.GRAY);
        targetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ice toggle "));
        targetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(targetText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(targetHelp);

        String timeText = IceBaoxue.getInstance().getConfig().getString("help.period", "Set the interval for each use of ICE Vape (unit: second)").replace("&", "§");
        TextComponent timeHelp = new TextComponent("/ice period <second>  " + timeText);
        timeHelp.setColor(ChatColor.GRAY);
        timeHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ice period "));
        timeHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(timeText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(timeHelp);

        String recorderText = IceBaoxue.getInstance().getConfig().getString("help.violent", "Let's take a bite of the super cool violent mode!").replace("&", "§");
        TextComponent recorderHelp = new TextComponent("/ice violent  " + recorderText);
        recorderHelp.setColor(ChatColor.GRAY);
        recorderHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ice violent "));
        recorderHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(recorderText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(recorderHelp);

        String reloadText = IceBaoxue.getInstance().getConfig().getString("help.reload", "Reload configuration.").replace("&", "§");
        TextComponent reloadHelp = new TextComponent("/ice reload  " + reloadText);
        reloadHelp.setColor(ChatColor.GRAY);
        reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ice reload"));
        reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(reloadHelp);
    }
}
